﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.Data;
using System.IO;
using System.Net;
using System.Text;

/// <summary>
/// Webfuncs are functions that involve sending and receiving requests from other servers.
/// </summary>
public static class WebFuncs
{
    /// <summary>
    /// Loads a text file from a different server.
    /// </summary>
    /// <param name="RequestURL">The full url of the requested file.</param>
    /// <param name="Username">Optional authentication information.</param>
    /// <param name="Password">Optional authentication information.</param>
    /// <returns></returns>
    public static string LoadTextFile(string RequestURL,string Username = "",string Password = "")
    {
        string results = "";

        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RequestURL);
            if(Username != "" && Password != "")
            {
                req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(Username + ":" + Password)));
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream());
            results = sr.ReadToEnd();
            sr.Close();
        }
        catch (Exception ex)
        {
            results = ex.Message;
        }
        return results;
    }

    /// <summary>
    /// Loads a binary file from a different server.
    /// </summary>
    /// <param name="RequestURL">The full url of the requested file.</param>
    /// <param name="Username">Optional authentication information.</param>
    /// <param name="Password">Optional authentication information.</param>
    /// <returns></returns>
    public static Byte[] LoadBinaryFile(string RequestURL, string Username = "", string Password = "")
    {
        Byte[] results;

        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RequestURL);
            if (Username != "" && Password != "")
            {
                req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(Username + ":" + Password)));
            }
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            BinaryReader readStream = new BinaryReader(resp.GetResponseStream());
            results = new Byte[resp.ContentLength];
            readStream.Read(results, 0,Convert.ToInt32(resp.ContentLength));
            readStream.Close();
        }
        catch (Exception ex)
        {
            results = null;
        }
        return results;
    }

    /// <summary>
    /// Loads a text file from a different server. Uses cookies for authentication.
    /// </summary>
    /// <param name="RequestURL">The full url of the requested file.</param>
    /// <param name="myCookies">Authentication information.</param>
    /// <returns></returns>
    public static string LoadTextFile(string RequestURL, CookieCollection myCookies)
    {
        string results = "";

        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RequestURL);
            req.CookieContainer.Add(myCookies);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(resp.GetResponseStream());
            results = sr.ReadToEnd();
            sr.Close();
        }
        catch (Exception ex)
        {
            results = ex.Message;
        }
        return results;
    }

    /// <summary>
    /// Loads a binary file from a different server. Uses cookies for authentication.
    /// </summary>
    /// <param name="RequestURL">The full url of the requested file.</param>
    /// <param name="myCookies">Authentication information.</param>
    /// <returns></returns>
    public static Byte[] LoadBinaryFile(string RequestURL, CookieCollection myCookies)
    {
        Byte[] results;

        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RequestURL);
            req.CookieContainer.Add(myCookies);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            BinaryReader readStream = new BinaryReader(resp.GetResponseStream());
            results = new Byte[resp.ContentLength];
            readStream.Read(results, 0, Convert.ToInt32(resp.ContentLength));
            readStream.Close();
        }
        catch (Exception ex)
        {
            results = null;
        }
        return results;
    }

}